package com.aier.admin_new.entity.mapper;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.admin_new.entity.User;


public class UserRowMapper implements RowMapper<User> {

    private int type;
    
    public static final int BRIEF=1;
    
    public static final int FANS=2;
    
    public static final int BABY_CLASSNAME=3;
    
    public static final int BABY_CLASSNAMEANDID=4;
    
    public static final int SERVICE = 5;
    
    public UserRowMapper() {
    }

    public UserRowMapper(int type) {
        this.type = type;
    }

    @Override
    public User mapRow(ResultSet rs, int rowNum) throws SQLException {

        User user = null;
        switch(type) {
            case BRIEF:
                user = parseUser(new User(), rs);
                user.setCfollow(rs.getInt("cfollow"));
                user.setCfans(rs.getInt("cfans"));
                user.setCfresh(rs.getInt("cfresh"));
                return user;
            case FANS:
                user = parseUser(new User(), rs);
                user.setCfollow(rs.getInt("cfollow"));
                user.setCfans(rs.getInt("cfans"));
                user.setCfresh(rs.getInt("cfresh"));
                user.setState(rs.getShort("state"));
                return user;
            case BABY_CLASSNAME:
                user = parseUser(new User(), rs);
                user.setBaby_className(rs.getString("baby_className"));
                user.setRelation(rs.getString("relation"));
                return user;
            case BABY_CLASSNAMEANDID:
                user = parseUser(new User(), rs);
                user.setBaby_className(rs.getString("baby_className"));
                user.setBaby_classId(rs.getLong("baby_classId"));                
                return user;
            case SERVICE:
                user = parseUser(new User(), rs);
                user.setEdate(rs.getString("edate"));
                return user;
            default:
                return parseUser(new User(), rs);
        }
        
    }

    public static User parseUser(User u, ResultSet rs) throws SQLException {
        return parseUser(u, rs, null);
    }

    public static User parseUser(User u, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            u.setUid(rs.getLong(su + "uid"));
        } catch (SQLException e) {
            u.setUid(null);
        }
        try {
            u.setAccount(rs.getString(su + "account"));
        } catch (SQLException e) {
            u.setAccount(null);
        }
        try {
            u.setPwd(rs.getString(su + "pwd"));
        } catch (SQLException e) {
            u.setPwd(null);
        }
        try {
            u.setNickname(rs.getString(su + "nickname"));
        } catch (SQLException e) {
            u.setNickname(null);
        }
        try {
            u.setRealname(rs.getString(su + "realname"));
        } catch (SQLException e) {
            u.setRealname(null);
        }
        try {
            u.setPhone(rs.getString(su + "phone"));
        } catch (SQLException e) {
            u.setPhone(null);
        }
        try {
            u.setEmail(rs.getString(su + "email"));
        } catch (SQLException e) {
            u.setEmail(null);
        }
        try {
            u.setCity_id(rs.getInt(su + "city_id"));
        } catch (SQLException e) {
            u.setCity_id(null);
        }
        try {
            u.setAddress(rs.getString(su + "address"));
        } catch (SQLException e) {
            u.setAddress(null);
        }
        try {
            u.setBirthday(rs.getDate(su + "birthday"));
        } catch (SQLException e) {
            u.setBirthday(null);
        }        
        try {
            u.setSex(rs.getString(su + "sex"));
        } catch (SQLException e) {
            u.setSex(null);
        }       
        try {
            u.setDesinfo(rs.getString(su + "desinfo"));
        } catch (SQLException e) {
            u.setDesinfo(null);
        }
        try {
            u.setConstellation(rs.getString(su + "constellation"));
        } catch (SQLException e) {
            u.setConstellation(null);
        }
        try {
            u.setInterest(rs.getString(su + "interest"));
        } catch (SQLException e) {
            u.setInterest(null);
        }
        try {
            u.setHeadimg(rs.getString(su + "headimg"));
        } catch (SQLException e) {
            u.setHeadimg(null);
        }
        try {
            u.setIsbaby(rs.getShort(su + "isbaby"));
        } catch (SQLException e) {
            u.setIsbaby(null);
        }
        try {
            u.setPhone_imei(rs.getString(su + "phone_imei"));
        } catch (SQLException e) {
            u.setPhone_imei(null);
        }
        try {
            u.setCoverimg(rs.getString(su + "coverimg"));
        } catch (SQLException e) {
            u.setCoverimg(null);
        }
        
        return u;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, User u) {
        par.put("uid", u.getUid());
        par.put("account", u.getAccount());
        par.put("pwd", u.getPwd());
        par.put("nickname", u.getNickname());
        par.put("realname", u.getRealname());
        par.put("phone", u.getPhone());
        par.put("email", u.getEmail());
        par.put("city_id", u.getCity_id());
        par.put("address", u.getAddress());
        par.put("birthday", u.getBirthday());
        par.put("sex", u.getSex());
        par.put("desinfo", u.getDesinfo());
        par.put("constellation", u.getConstellation());
        par.put("interest", u.getInterest());
        par.put("headimg", u.getHeadimg());
        par.put("isbaby", u.getIsbaby());
        par.put("phone_imei", u.getPhone_imei());
        par.put("coverimg", u.getCoverimg());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, User u) {
        args.put("uid", u.getUid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final User u) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(u),
                        new String[] { "uid" });
                parsePS(ps, u);
                return ps;
            }

        };
    }

    private static String parseSQL(User u) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into user(");
        int count = 0;
        if (null != u.getAccount()) {
            sql.append("account,");
            count++;
        }
        if (null != u.getPwd()) {
            sql.append("pwd,");
            count++;
        }
        if (null != u.getNickname()) {
            sql.append("nickname,");
            count++;
        }
        if (null != u.getRealname()) {
            sql.append("realname,");
            count++;
        }
        if (null != u.getPhone()) {
            sql.append("phone,");
            count++;
        }
        if (null != u.getEmail()) {
            sql.append("email,");
            count++;
        }
        if (null != u.getCity_id()) {
            sql.append("city_id,");
            count++;
        }
        if (null != u.getAddress()) {
            sql.append("address,");
            count++;
        }
        if (null != u.getBirthday()) {
            sql.append("birthday,");
            count++;
        }
        if (null != u.getSex()) {
            sql.append("sex,");
            count++;
        }
        if (null != u.getDesinfo()) {
            sql.append("desinfo,");
            count++;
        }
        if (null != u.getConstellation()) {
            sql.append("constellation,");
            count++;
        }
        if (null != u.getInterest()) {
            sql.append("interest,");
            count++;
        }
        if (null != u.getHeadimg()) {
            sql.append("headimg,");
            count++;
        }
        if (null != u.getIsbaby()) {
            sql.append("isbaby,");
            count++;
        }
        if (null != u.getPhone_imei()) {
            sql.append("phone_imei,");
            count++;
        }
        
        if (null != u.getCoverimg()) {
            sql.append("coverimg,");
            count++;
        }
        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, User u)
        throws SQLException {
        int count = 1;
        if (null != u.getAccount()) {
            ps.setString(count++, u.getAccount());
        }
        if (null != u.getPwd()) {
            ps.setString(count++, u.getPwd());
        }
        if (null != u.getNickname()) {
            ps.setString(count++, u.getNickname());
        }
        if (null != u.getRealname()) {
            ps.setString(count++, u.getRealname());
        }
        if (null != u.getPhone()) {
            ps.setString(count++, u.getPhone());
        }
        if (null != u.getEmail()) {
            ps.setString(count++, u.getEmail());
        }
        if (null != u.getCity_id()) {
            ps.setInt(count++, u.getCity_id());
        }
        if (null != u.getAddress()) {
            ps.setString(count++, u.getAddress());
        }
        if (null != u.getBirthday()) {
            ps.setDate(count++, (Date) u.getBirthday());
        }
        if (null != u.getSex()) {
            ps.setString(count++, u.getSex());
        }
        if (null != u.getDesinfo()) {
            ps.setString(count++, u.getDesinfo());
        }
        if (null != u.getConstellation()) {
            ps.setString(count++, u.getConstellation());
        }
        if (null != u.getInterest()) {
            ps.setString(count++, u.getInterest());
        }
        if (null != u.getHeadimg()) {
            ps.setString(count++, u.getHeadimg());
        }
        if (null != u.getIsbaby()) {
            ps.setShort(count++, u.getIsbaby());
        }
        
        if (null != u.getPhone_imei()) {
            ps.setString(count++, u.getPhone_imei());
        }
        
        if (null != u.getCoverimg()) {
            ps.setString(count++, u.getCoverimg());
        }
    }
}
